package com.lanou.dllo.day14;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by dllo on 16/1/26.
 */
public class MainAdapter extends RecyclerView.Adapter {

    private Context context;
    private MyBean myBean;
    private LayoutInflater inflater;


    public MainAdapter(Context context) {
        this.context = context;
        myBean = new MyBean();
        inflater = LayoutInflater.from(context);
    }

    //该方法 就是通过数据类来判断需要加载的布局类型
    //返回int值,该值是在数据类里我们自己写的
    @Override
    public int getItemViewType(int position) {

        return myBean.getType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case 0:
                View receiveView = inflater.inflate(R.layout.itemreceive, parent, false);
                viewHolder = new ViewHolderReceive(receiveView);
                break;
            case 1:
                View sendView = inflater.inflate(R.layout.itemsend, parent, false);
                viewHolder = new ViewHolderSend(sendView);
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        //获得当前item的类型
        int viewType = getItemViewType(position);
        switch (viewType){
            case 0:
                ViewHolderReceive viewHolderReceive = (ViewHolderReceive) holder;
                viewHolderReceive.receive.setText(myBean.getData(position));
                break;
            case 1 :
                ((ViewHolderSend) holder).send.setText(myBean.getData(position));
                break;
        }
    }

    @Override
    public int getItemCount() {
        return myBean.getCount();
    }

    //加载发送布局的
    class ViewHolderSend extends RecyclerView.ViewHolder {
        TextView send;

        public ViewHolderSend(View itemView) {
            super(itemView);
            send = (TextView) itemView.findViewById(R.id.send);
        }
    }

    //加载接收布局的
    class ViewHolderReceive extends RecyclerView.ViewHolder {
        TextView receive;

        public ViewHolderReceive(View itemView) {
            super(itemView);

            receive = (TextView) itemView.findViewById(R.id.receive);
        }
    }
}
